﻿Ext.define("GS.controller.Post", {
    extend: "Ext.app.Controller",

    config: {
        refs: {
            mainView: "main",
            viewPost: "viewpost",
            addPostView: "addpost",
            commentView: "addcomment",
            btnPost: "#btnPost",
            PostForm: '#postform',
            postIndex: '#postindex',
            postList: '#postlist',
            mypostList: '#mypostlist',
            btnBackViewPost: "#btnBackViewPost",
            btnComment: "#btnComment",
            btnCancelPost: "#btnCancelPost",
            btnAddPost: "#btnAddPost",
        },
        control: {
            btnPost: {
                tap: "onPost"
            },
            postList: {
                itemtap: 'showPostDetail'
            },
            mypostList: {
                itemtap: 'showPostDetail'
            },
            btnBackViewPost: {
                tap: "onBackViewPost"
            },
            btnComment: {
                tap: "onComment"
            },
            btnCancelPost: {
                tap: "onCancelPost"
            },
            btnAddPost: {
                tap: "onAddPost"
            }
        }
    },
    onAddPost: function () {
        Ext.Viewport.setActiveItem(this.getAddPostView());
    },
    onComment: function () {
        var post_id = this.getViewPost().getPostId();
        this.getCommentView().updatePostId(post_id);
        Ext.Viewport.setActiveItem(this.getCommentView());
    },
    onBackViewPost: function () {
        Ext.Viewport.setActiveItem(this.getMainView());
    },
    onCancelPost: function (id) {
        Ext.Viewport.setActiveItem(this.getMainView());
    },
    onPost: function () {
        var values = this.getPostForm().getValues();
        var now = new Date();

        var user = Ext.create('GS.store.UserData');

        user.load();

        var user_id = user.getAt(0).get('user_id');

        var category_id = this.getPostIndex().config.category_id;

        var model = Ext.create('GS.model.post.Post', values);
                                
        var errors = model.validate();
        if(errors.isValid()){
            Ext.data.JsonP.request({
                url: GS.util.Config.getServerUrl()+'AddPost.php',
                callbackKey: "callback",
                params: { category_id: category_id, user_id: user_id, title: values.title, body: values.body, date_created: now, date_last_update: now },

                success: function (response) {
                    var text = response.responseText;
                    Ext.Msg.alert('Success', text);
                },

                failure: function (response) {
                    Ext.Msg.alert('Error', 'Error while submitting the form');
                    //console.log(response.responseText);
                }
            });                   
        } else {
            var errorMessage = "";
            Ext.each(errors.items,function(rec,i){
                errorMessage += rec.config.message+"<br/>";
            });
            Ext.Msg.alert("Fejl", errorMessage, function(){});
            return false;
        }

        
    },
    showPostDetail: function (list, index, element, record) {
        this.getViewPost().updatePost(record.data);
        Ext.Viewport.setActiveItem(this.getViewPost());
    }
});